package com.nnnu.wsnackshop.controller.user;

import com.nnnu.wsnackshop.constant.MessageConstants;
import com.nnnu.wsnackshop.pojo.vo.ProductTagsVO;
import com.nnnu.wsnackshop.pojo.vo.TagVO;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.IProductTagsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户端-商品标签关联 前端控制器
 * </p>
 *
 * @author zk
 * @since 2025-05-14
 */
@RestController("userProductTagsController")
@RequestMapping("/api/user/product-tags")
@Tag(name = "用户端-商品标签关联接口")
@Slf4j
@Validated
@RequiredArgsConstructor
public class ProductTagsController {

    private final IProductTagsService productTagsService;

    @Operation(summary = "查询商品已关联标签", description = "获取指定商品的所有标签信息")
    @GetMapping("/list/{productId}")
    @Cacheable(cacheNames = "userProductTags", key = "'product:' + #productId")
    public Result<ProductTagsVO> listByProduct(
            @PathVariable @Parameter(description = "商品 ID", in = ParameterIn.PATH) Long productId) {
        log.info("[用户端] 查询商品({})已关联标签", productId);
        List<TagVO> tags = productTagsService.listTagsByProduct(productId);
        ProductTagsVO vo = new ProductTagsVO();
        vo.setProductId(productId);
        vo.setTags(tags);
        return Result.success(vo, MessageConstants.SELECT_OK);
    }

    @Operation(summary = "查询多个商品的标签信息", description = "批量获取多个商品的标签信息")
    @PostMapping("/batch/list")
    public Result<List<ProductTagsVO>> batchListByProducts(
            @RequestBody @Parameter(description = "商品ID列表") List<Long> productIds) {
        log.info("[用户端] 批量查询商品标签，商品数量：{}", productIds.size());
        List<ProductTagsVO> result = productIds.stream()
                .map(productId -> {
                    List<TagVO> tags = productTagsService.listTagsByProduct(productId);
                    ProductTagsVO vo = new ProductTagsVO();
                    vo.setProductId(productId);
                    vo.setTags(tags);
                    return vo;
                })
                .toList();
        return Result.success(result, MessageConstants.SELECT_OK);
    }

    @Operation(summary = "查询热门标签", description = "获取系统中使用频率最高的标签")
    @GetMapping("/popular")
    @Cacheable(cacheNames = "userPopularTags", key = "'popular'")
    public Result<List<TagVO>> getPopularTags(
            @RequestParam(defaultValue = "10") @Parameter(description = "返回数量，默认10") Integer limit) {
        log.info("[用户端] 查询热门标签，数量：{}", limit);
        // 这里需要根据实际业务需求实现热门标签的查询逻辑
        // 可能需要添加新的Service方法
        List<TagVO> popularTags = productTagsService.getPopularTags(limit);
        return Result.success(popularTags, MessageConstants.SELECT_OK);
    }
}
